package com.example.ijkplayer_sdk;

import android.content.Context;
import android.media.Image;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

import com.example.ijkplayer_sdk.media.IjkVideoView;

public class IjkPlayerView extends FrameLayout implements LifecycleObserver {

    private IjkVideoView ijkVideoView;
    private RelativeLayout relativeLayout;
    private ImageView imageView;
    private ImageView playimg;


    public IjkPlayerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        AppCompatActivity appCompatActivity= (AppCompatActivity) context;
        appCompatActivity.getLifecycle().addObserver(this);
        init();
    }

    private void init() {
        ijkVideoView=new IjkVideoView(getContext());
        imageView = new ImageView(getContext());
        relativeLayout = new RelativeLayout(getContext());
        playimg = new ImageView(getContext());
        FrameLayout.LayoutParams ijkVideoLp=new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
        ijkVideoView.setLayoutParams(ijkVideoLp);
        FrameLayout.LayoutParams imgLp=new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
        imageView.setLayoutParams(imgLp);
        FrameLayout.LayoutParams relativeLp=new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
        relativeLayout.setLayoutParams(relativeLp);
        RelativeLayout.LayoutParams playImgLp=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        playImgLp.addRule(RelativeLayout.CENTER_IN_PARENT);
        playimg.setLayoutParams(playImgLp);
        playimg.setImageResource(R.drawable.icon_video_play);
        relativeLayout.addView(playimg);
        addView(ijkVideoView);
        addView(imageView);
        addView(relativeLayout);
//        cover
    }
//    设置播放路径
    public void setPlayUrl(String url){
        if (ijkVideoView==null)
            return;
        ijkVideoView.setVideoPath(url);
    }
    //开始播放
    public void startPlay(){
        if (ijkVideoView==null)
            return;
        ijkVideoView.start();
    }
//   暂停播放
    public void pausePlay(){
        if (ijkVideoView==null)
            return;
        if (ijkVideoView.canPause())
            ijkVideoView.pause();
    }

//    隐藏封面
    public void setImageCoverVisibility(int visibility ){
        if (imageView==null)
            return;
        imageView.setVisibility(visibility);
    }
//    影藏控件
    public void setMediaViewVisibility(int visibility ){
        if (imageView==null)
            return;
        relativeLayout.setVisibility(visibility);
    }

//    是否播放状态校验
    public boolean checkPlaying(){
        if (ijkVideoView==null)
            return false;
        return ijkVideoView.isPlaying();
    }


//    暂停ijkPlayer播放
    @OnLifecycleEvent(value = Lifecycle.Event.ON_PAUSE)
    public void onPause(){
        pausePlay();
    }

//    继续ijkPlayer播放
@OnLifecycleEvent(value = Lifecycle.Event.ON_RESUME)
    public void onResume(){
        startPlay();
    }

    //销毁
    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void onDestroy(){
        AppCompatActivity appCompatActivity= (AppCompatActivity) getContext();
        appCompatActivity.getLifecycle().removeObserver(this);
        //释放播放器
        ijkVideoView.release(true);
        Log.e("GJD","onDestroy()");
    }
}
